#include "fuStringTokenizer.h"

BEGIN_NAMESPACE_CPPX

const std::string fuStringTokenizer::DEFAULT_DELIMITERS(" ") ;

fuStringTokenizer::fuStringTokenizer(const std::string& str) :
m_String(str),
m_Delimiters(DEFAULT_DELIMITERS),
m_Offset(0)
{}

fuStringTokenizer::fuStringTokenizer(const std::string& str, const std::string& delimiters) :
m_String(str),
m_Delimiters(delimiters),
m_Offset(0)
{}

bool fuStringTokenizer::nextToken(void)
{
    return nextToken(m_Delimiters);
}

bool fuStringTokenizer::nextToken(const std::string& delimiters)
{
    // find the start charater of the next token.
    size_t i = m_String.find_first_not_of(delimiters, m_Offset);
    if (i == std::string::npos) {
        m_Offset = m_String.length();
        return false;
    }

    // find the end of the token.
    size_t j = m_String.find_first_of(delimiters, i);
    if (j == std::string::npos) {
        m_Token = m_String.substr(i);
        m_Offset = m_String.length();
        return true;
    }

    // to intercept the token and save current position
    m_Token = m_String.substr(i, j - i);
    m_Offset = j;
    return true;
}

const std::string fuStringTokenizer::getToken(void) const
{
    return m_Token;
}

void fuStringTokenizer::reset()
{
    m_Offset = 0;
}

END_NAMESPACE_CPPX
